package com.ossjk.qlh.system.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.system.service.ILoginfoService;
import com.ossjk.qlh.system.service.IUserService;
import com.ossjk.qlh.system.vo.LoginfoVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 登录记录表 前端控制器
 * </p>
 *
 * @author chair
 * @since 2020-01-30
 */
@Api(tags = "日志信息")
@RestController
@RequestMapping("/system/loginfo")
public class LoginfoController extends BaseController {

	@Autowired
	private ILoginfoService iLoginfoService;
	@Autowired
	private IUserService iUserService;

	@ApiOperation(value = "列表")
	@ApiImplicitParams({ @ApiImplicitParam(name = "uname", value = "用户名", paramType = "query"), @ApiImplicitParam(name = "module", value = "模块", paramType = "query"), @ApiImplicitParam(name = "operation", value = "操作", paramType = "query"), @ApiImplicitParam(name = "type", value = "日志类型 1-业务、2-错误信息", paramType = "query"), @ApiImplicitParam(name = "beginTime", value = "开始时间", paramType = "query"), @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query") })
	@PostMapping(value = "/list")
	@RequiresPermissions("00000030")
	public ResponseBean<Page<LoginfoVo>> list(@RequestBody RequestBean requestBean) {
		QueryWrapper<LoginfoVo> queryWrapper = new QueryWrapper<LoginfoVo>();
		String uname = (String) requestBean.getParam().get("uname");
		String module = (String) requestBean.getParam().get("module");
		String operation = (String) requestBean.getParam().get("operation");
		String type = (String) requestBean.getParam().get("type");
		String beginTime = (String) requestBean.getParam().get("beginTime");
		String endTime = (String) requestBean.getParam().get("endTime");
		if (!CommonUtil.isBlank(uname)) {
			queryWrapper.like("u.name", uname);
		}
		if (!CommonUtil.isBlank(module)) {
			queryWrapper.like("l.module", module);
		}
		if (!CommonUtil.isBlank(operation)) {
			queryWrapper.like("l.operation", operation);
		}
		if (!CommonUtil.isBlank(type)) {
			queryWrapper.eq("l.type", type);
		} else {
			// 默认系统日志
			queryWrapper.eq("l.type", 1);
		}
		if (!CommonUtil.isBlank(beginTime)) {
			queryWrapper.gt("l.optime", beginTime);
		}
		if (!CommonUtil.isBlank(endTime)) {
			queryWrapper.lt("l.optime", endTime);
		}
		queryWrapper.orderByDesc("l.optime");
		return responseSuccess(iLoginfoService.pageVo(requestBean.getPage(), queryWrapper));
	}

	@ApiOperation(value = "详细")
	@PostMapping(value = "/detail")
	@RequiresPermissions("00000030")
	public ResponseBean<LoginfoVo> detail(@RequestBody RequestBean requestBean) {
		return responseSuccess(iLoginfoService.getVoById((String) requestBean.getParam().get("id")));
	}
}
